<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;

        }

        .outer {
            width: 400px;
            height: 500px;
            margin: 50px auto;
            position: relative;
        }

        .imgs_list {
            list-style: none;
            width: inherit;
            height: inherit;
        }

        img {
            width: 400px;
            height: 500px;
            vertical-align: middle;
        }

        .imgs_list li {
            position: absolute;
            opacity: 0;
            transition: opacity 0.6s;
        }

        .imgs_list .current {
            z-index: 1;
            opacity: 1;
        }

        .change a {
            position: absolute;
            background-color: rgba(0, 0, 0, 0.6);
            width: 20px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            text-decoration: none;
            bottom: 50%;
            opacity: 0.5;
            z-index: 999;
        }

        .change a:hover {
            opacity: 1;
        }

        .change .next {
            right: 0;
        }

        .change .prev {
            left: 0;
        }

        .change i {
            font-weight: bold;
            font-size: 20px;
            text-align: center;
            color: rgba(255, 255, 255, 0.8);

        }

        /* 导航点 */

        .navg_dots {
            position: absolute;
            bottom: 20px;
            left: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            z-index: 3;
            height: 20px;
            width: 100px;

        }

        .navg_dots a {
            opacity: 0.5;
            color: #fff;
            font-size: 11px;
        }

        .navg_dots a:hover,
        .navg_dots .active {
            opacity: 1;
        }
    </style>
    <link rel="stylesheet" href="../fontawesome/css/all.css">
    <script>
        document.addEventListener("DOMContentLoaded", () => {

            const toggleChange = (() => {
                let timer = null
                return () => {
                    if (timer === null) {
                        timer = setTimeout(auto = () => {
                            changeImg("next")
                            timer = setTimeout(auto, 3000)
                        }, 3000)
                    }
                    else {
                        clearTimeout(timer)
                        timer = null
                    }
                }
            })();

            toggleChange();

            // 获取外层容器
            const outer = document.querySelectorAll(".outer")[0];

            outer.addEventListener("mouseenter", () => {
                toggleChange()
            });

            outer.addEventListener("mouseleave", () => {
                toggleChange()
            });


            // 点击按钮，切换图片

            const prev = document.querySelector(".change .prev")
            const next = document.querySelector(".change .next")

            prev.addEventListener("click", () => {
                event.preventDefault();
                changeImg("prev")

            });

            next.addEventListener("click", () => {
                event.preventDefault();
                changeImg("next")
            });

            const dots = document.querySelector(".navg_dots");

            dots.addEventListener("click", (event) => {
                if (event.target.tagName === "A") {
                    event.preventDefault();
                    const index = Array.from(dots.children).indexOf(event.target);
                    changeImg(index);
                }
            });
            /* 
                change_img用来切换图片
                    参数direction   用来表示切换的方向。
                        next
                        prev
    
            */
            function changeImg(direction) {
                // 获取当前显示图片
                const current = document.querySelectorAll(".imgs_list .current")[0]
                const imgArr = Array.from(document.querySelectorAll(".imgs_list li"))

                // 获取当前显示图片的索引

                const index = imgArr.indexOf(current);
                let next;
                if (direction == "next") {
                    next = current.nextElementSibling || imgArr[0]
                }
                else if (direction == "prev") {
                    next = current.previousElementSibling || imgArr.at(-1)
                }
                else if (typeof direction === "number")
                    next = imgArr[direction]

                current.classList.remove("current")
                next.classList.add("current")
                //切换 active
                const currentActive = document.querySelector(".navg_dots .active")
                currentActive.classList.remove("active")
                // 获取当前要显示的点
                dots[imgArr.indexOf(next)].classList.add("active")
            }
        });

    </script>
</head>

<body>
    <div class="outer">
        <ul class="imgs_list">
            <li class="current">
                <a href="javascript:"><img src="../10_DOM/images/IMG_1.JPG" alt="juilet"></a>
            </li>
            <li>
                <a href="javascript:"><img src="../10_DOM/images/IMG_2.JPG" alt="juilet"></a>
            </li>
            <li>
                <a href="javascript:"><img src="../10_DOM/images/IMG_3.JPG" alt="juilet"></a>
            </li>
            <li>
                <a href="javascript:"><img src="../10_DOM/images/IMG_4.JPG" alt="juilet"></a>
            </li>
            <li>
                <a href="javascript:"><img src="../10_DOM/images/IMG_5.JPG" alt="juilet"></a>
            </li>
        </ul>
        <div class="change">
            <a class="prev" href="javascript:"><i class="fa-solid fa-angle-left"></i></a>
            <a class="next" href="javascript:"><i class="fa-solid fa-angle-right"></i></a>
        </div>
        <div class="navg_dots">
            <a class="active" href="javascript:"><i class="fa-solid fa-circle"></i></a>
            <a href="javascript:"><i class="fa-solid fa-circle"></i></a>
            <a href="javascript:"><i class="fa-solid fa-circle"></i></a>
            <a href="javascript:"><i class="fa-solid fa-circle"></i></a>
            <a href="javascript:"><i class="fa-solid fa-circle"></i></a>
        </div>
    </div>
</body>

</html>